'选择菜单，控制游戏的选项'

extends CanvasLayer

@onready var window_button: Button = %WindowButton
@onready var sfx_slider: HSlider = %SfxSlider
@onready var music_slider: HSlider = %MusicSlider
@onready var language_button: Button = %LanguageButton
@onready var move_button: Button = %MoveButton

func _ready() -> void:
	window_button.pressed.connect(on_window_pressed)
	%BackButton.pressed.connect(on_back_pressed)
	language_button.pressed.connect(on_language_pressed)
	move_button.pressed.connect(on_movemode_pressed)
	sfx_slider.value_changed.connect(on_value_changed.bind("sfx"))
	music_slider.value_changed.connect(on_value_changed.bind("music"))
	set_options()
	update_display()
	pass

#进游戏时加载选项的内容
func set_options() -> void:
	TranslationServer.set_locale(GameSaves.get_option_data("language"))
	var bus_index_sfx = AudioServer.get_bus_index("sfx")
	var db_sfx = linear_to_db(min(GameSaves.get_option_data("sfx"),1))
	AudioServer.set_bus_volume_db(bus_index_sfx, db_sfx)
	var bus_index_music = AudioServer.get_bus_index("music")
	var db_music = linear_to_db(min(GameSaves.get_option_data("music"),1))
	AudioServer.set_bus_volume_db(bus_index_music, db_music)
	if GameSaves.get_option_data("window_mode") == "Fullscreen":
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
	else:
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
	pass


#更新展示内容
func update_display() -> void:
	if GameSaves.get_option_data("move_mode") == 1:
		move_button.text = "keyboard"
	else:
		move_button.text = "mouse"
	window_button.text = GameSaves.get_option_data("window_mode")
	if GameSaves.get_option_data("language") == "zh":
		language_button.text = "简体中文"
	else:
		language_button.text = "English"
	sfx_slider.value = get_bus_volume_percent("sfx")
	music_slider.value = get_bus_volume_percent("music")
	if self.get_parent().name == "PasueMenu":
		$MarginContainer/PanelContainer/MarginContainer/VBoxContainer\
		/ScrollContainer/MarginContainer/VBoxContainer3/MoveMode.hide()
		$TextureRect.hide()
	self.visible = true
	pass

#返回对应音量百分比
func get_bus_volume_percent(bus_name: String) -> float:
	var bus_index = AudioServer.get_bus_index(bus_name)
	var volume_db = AudioServer.get_bus_volume_db(bus_index)
	return db_to_linear(volume_db)

#设置对应音量百分比
func set_bus_volume_percent(bus_name: String, bus_volume: float) -> void:
	var bus_index = AudioServer.get_bus_index(bus_name)
	var db = linear_to_db(bus_volume)
	AudioServer.set_bus_volume_db(bus_index, db)
	pass

#设置窗口模式
func on_window_pressed() -> void:
	var mode = DisplayServer.window_get_mode()
	if mode != DisplayServer.WINDOW_MODE_FULLSCREEN:
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_FULLSCREEN)
		window_button.text = "Fullscreen"
		GameSaves.set_options_data("window_mode","Fullscreen")
	else:
		DisplayServer.window_set_flag(DisplayServer.WINDOW_FLAG_BORDERLESS, false)
		DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_WINDOWED)
		window_button.text = "Windowed"
		GameSaves.set_options_data("window_mode","Windowed")
	pass

#设置语言
func on_language_pressed() -> void:
	var languange_name = TranslationServer.get_locale()
	if languange_name == "zh":
		TranslationServer.set_locale("en")
		GameSaves.set_options_data("language","en")
		language_button.text = "English"
	else:
		TranslationServer.set_locale("zh")
		GameSaves.set_options_data("language","zh")
		language_button.text = "简体中文"
	pass

#设置移动方式
func on_movemode_pressed() -> void:
	if GameSaves.get_option_data("move_mode") == 1:
		GameSaves.set_options_data("move_mode",2)
		move_button.text = "mouse"
	elif GameSaves.get_option_data("move_mode") == 2:
		GameSaves.set_options_data("move_mode",1)
		move_button.text = "keyboard"
	pass

#当音量滑条改变时调用
func on_value_changed(value: float, bus_name: String) -> void:
	set_bus_volume_percent(bus_name, value)
	GameSaves.set_options_data(bus_name,value)
	pass

#返回按钮，隐藏自己
func on_back_pressed() -> void:
	self.hide()
	self.get_parent().visible = true
	pass
